package com.bluegalaxy.nhb.snservice.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;

import javax.annotation.Resource;
import javax.sql.DataSource;

//@Configuration
//@MapperScan(basePackages = "com.bluegalaxy.nhb.snservice.daomysql", sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MysqlDruidConfig {


//    @Bean(name = "anDataSource")
//    @Qualifier("anDataSource")
//    @Primary
//    @ConfigurationProperties(prefix="spring.datasource.druid")
//    public DataSource anDataSource() {
//        return DataSourceBuilder.create().build();
//    }
//
//    @Bean(name = "anJdbcTemplate")
//    public JdbcTemplate anJdbcTemplate(@Qualifier("anDataSource") DataSource dataSource) {
//        return new JdbcTemplate(dataSource);
//    }

//    @Bean
//    public DataSource dataSource() {
//        DruidDataSource datasource = new DruidDataSource();
//        datasource.setUrl(jdbcParamConfig.getUrl());
//        datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());
//        datasource.setInitialSize(jdbcParamConfig.getInitialSize());
//        datasource.setMinIdle(jdbcParamConfig.getMinIdle());
//        datasource.setMaxActive(jdbcParamConfig.getMaxActive());
//        datasource.setMaxWait(jdbcParamConfig.getMaxWait());
//        datasource.setUsername(jdbcParamConfig.getUsername());
//        datasource.setPassword(jdbcParamConfig.getPassword());
//        return datasource;
//    }
// 注入数据源配置
//    @Primary
//    @Bean
//    @ConfigurationProperties(prefix = "spring.datasource.druid")
//    public DataSourceProperties mysqlDataSourceProperties() {
//        return new DataSourceProperties();
//    }
//
//    // 创建数据库连接
//    @Primary
//    @Bean
//    public DataSource mysqlDataSource() {
//        return mysqlDataSourceProperties().initializeDataSourceBuilder().build();
//    }
//
//    // 创建session工厂
//    @Primary
//    @Bean
//    public SqlSessionFactory mysqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
//        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
//        sessionFactoryBean.setDataSource(dataSource);
//        sessionFactoryBean.setMapperLocations(
//                new PathMatchingResourcePatternResolver().getResources("classpath*:mappermysql/*.xml"));
//        return sessionFactoryBean.getObject();
//    }
//
//    // 创建事务管理（按需添加）
//    @Primary
//    @Bean
//    public DataSourceTransactionManager mysqlDataSourceTransactionManager() {
//        return new DataSourceTransactionManager(mysqlDataSource());
//    }
//
//    // 会话管理
//    @Primary
//    @Bean
//    public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }

}
